import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod_test/view/cart/cart_info_page.dart';
import 'package:flutter_riverpod_test/view/home/home_page.dart';

enum AppRoute {
  home(name: "/", page: HomePage()),
  cartInfo(name: "/cartInfo", page: CartInfoPage()), //argument: cart对象
  ;

  const AppRoute({required this.name, required this.page});

  final String name;
  final Widget page;

  Route get route => CupertinoPageRoute(builder: (BuildContext context) {
    return page;
  });
}

class AppNavigator {
  static GlobalKey<NavigatorState> navigatorKey = GlobalKey();

  static NavigatorState? get state => navigatorKey.currentState;

  static final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();//使用 RouteAware 对路由进行监听

  static Object? lastArguments; //记录最近一次的传递参数
  
  static Route onGenerateRoute(RouteSettings settings) {
    lastArguments = settings.arguments;
    Route? route;
    for(final item in AppRoute.values) {
      if (item.name == settings.name) {
        route = item.route;
        break;
      }
    }
    return route ??= AppRoute.home.route;
  }

  static Future? push<T>(AppRoute route, [T? arguments]) =>
      state?.pushNamed(route.name, arguments: arguments);

  static Future? replaceWith<T>(AppRoute route, [T? arguments]) =>
      state?.pushReplacementNamed(route.name, arguments: arguments);

  static void pop<T>([ T? result ]) => state?.pop(result);
  
  Future<T?> dialog<T>(WidgetBuilder builder) async {
    if (AppNavigator.navigatorKey.currentContext == null) {
      return null;
    }
    return showDialog<T>(
      context: AppNavigator.navigatorKey.currentContext!, 
      builder: builder
    );
  }
}

//缩放路由动画
class ScaleRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  ScaleRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
    pageBuilder: (context, animation, secondaryAnimation) => child,
    transitionDuration: Duration(milliseconds: duration),
    transitionsBuilder: (context, a1, a2, child) => ScaleTransition(
      scale: Tween(begin: 0.0, end: 1.0)
          .animate(CurvedAnimation(parent: a1, curve: curve)),
      child: child,
    ),
  );
}

//渐变透明路由动画
class FadeRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  FadeRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
      pageBuilder: (context, animation, secondaryAnimation) => child,
      transitionDuration: Duration(milliseconds: duration),
      transitionsBuilder: (context, a1, a2, child) => FadeTransition(
        opacity: Tween(begin: 0.1, end: 1.0).animate(CurvedAnimation(
          parent: a1,
          curve: curve,
        )),
        child: child,
      ));
}

//旋转路由动画
class RotateRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  RotateRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
      pageBuilder: (context, animation, secondaryAnimation) => child,
      transitionDuration: Duration(milliseconds: duration),
      transitionsBuilder: (context, a1, a2, child) => RotationTransition(
        turns: Tween(begin: 0.1, end: 1.0).animate(CurvedAnimation(
          parent: a1,
          curve: curve,
        )),
        child: child,
      ));
}

//上--->下
class Top2BottomRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  Top2BottomRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
      transitionDuration: Duration(milliseconds: duration),
      pageBuilder: (ctx, a1, a2) {
        return child;
      },
      transitionsBuilder: (
          ctx,
          a1,
          a2,
          Widget child,
          ) {
        return SlideTransition(
            position: Tween<Offset>(
              begin: const Offset(0.0, -1.0),
              end: const Offset(0.0, 0.0),
            ).animate(CurvedAnimation(parent: a1, curve: curve)),
            child: child);
      });
}

//左--->右
class Left2RightRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  Left2RightRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : assert(true),
        super(
          transitionDuration: Duration(milliseconds: duration),
          pageBuilder: (ctx, a1, a2) {
            return child;
          },
          transitionsBuilder: (
              ctx,
              a1,
              a2,
              Widget child,
              ) {
            return SlideTransition(
                position: Tween<Offset>(
                  begin: const Offset(-1.0, 0.0),
                  end: const Offset(0.0, 0.0),
                ).animate(CurvedAnimation(parent: a1, curve: curve)),
                child: child);
          });
}

//下--->上
class Bottom2TopRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  Bottom2TopRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
      transitionDuration: Duration(milliseconds: duration),
      pageBuilder: (ctx, a1, a2) {
        return child;
      },
      transitionsBuilder: (
          ctx,
          a1,
          a2,
          Widget child,
          ) {
        return SlideTransition(
            position: Tween<Offset>(
              begin: const Offset(0.0, 1.0),
              end: const Offset(0.0, 0.0),
            ).animate(CurvedAnimation(parent: a1, curve: curve)),
            child: child);
      });
}

//右--->左
class Right2LeftRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  Right2LeftRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
      transitionDuration: Duration(milliseconds: duration),
      pageBuilder: (ctx, a1, a2) {
        return child;
      },
      transitionsBuilder: (
          ctx,
          a1,
          a2,
          Widget child,
          ) {
        return SlideTransition(
            position: Tween<Offset>(
              begin: const Offset(1.0, 0.0),
              end: const Offset(0.0, 0.0),
            ).animate(CurvedAnimation(parent: a1, curve: curve)),
            child: child);
      });
}

//缩放+透明+旋转路由动画
class ScaleFadeRotateRouter<T> extends PageRouteBuilder<T> {
  final Widget child;
  final int duration;
  final Curve curve;
  ScaleFadeRotateRouter(
      {required this.child, this.duration = 500, this.curve = Curves.fastOutSlowIn})
      : super(
      transitionDuration: Duration(milliseconds: duration),
      pageBuilder: (ctx, a1, a2) => child, //页面
      transitionsBuilder: (
          ctx,
          a1,
          a2,
          Widget child,
          ) {
        //构建动画
        return RotationTransition(
          //旋转动画
          turns: Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
            parent: a1,
            curve: curve,
          )),
          child: ScaleTransition(
            //缩放动画
            scale: Tween(begin: 0.0, end: 1.0)
                .animate(CurvedAnimation(parent: a1, curve: curve)),
            child: FadeTransition(
              opacity: //透明度动画
              Tween(begin: 0.5, end: 1.0)
                  .animate(CurvedAnimation(parent: a1, curve: curve)),
              child: child,
            ),
          ),
        );
      });
}

//无动画
class NoAnimRouter<T> extends PageRouteBuilder<T> {
  final Widget page;
  NoAnimRouter(this.page)
      : super(
      opaque: false,
      pageBuilder: (context, animation, secondaryAnimation) => page,
      transitionDuration: const Duration(milliseconds: 0),
      transitionsBuilder:
          (context, animation, secondaryAnimation, child) => child);
}